<?php
	class calendar{
		
		var $actualDay //numerical representation of the actual day
			,$actualMon	//numerical representation of the actual month
			,$week	//array with textual representation for the week days
			,$days	//numerical representation of the month days
			,$months	//array with textual representations of the months
			,$year	//numerical representation of the actual year
			,$enlace; //page where is running 
		
		function calendar(){
			$this->year=date("Y",time());
			$this->months=array('Enero','Febrero','Marzo','Abril','Mayo','Junio',
								'Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre');
			$this->week=array('Domingo','Lunes','Martes','Miercoles','Jueves','Viernes','Sabado');
			$this->days=date("t",time());
			$this->actualMon=date("n",time());
			$this->actualDay=date("j",time());
		}
		
		function setEnlace($e){
			$this->enlace = $e;
		}
		
		function setYear($y){
			$this->year=$y;
		}
		
		function setactualMon($m){
			$this->actualMon=$m;
			$ultimo_dia=28; 
			while (checkdate($m,$ultimo_dia + 1,$this->year)){ 
			   $ultimo_dia++; 
			} 
			$this->days=$ultimo_dia;
		}
		
		function drawCalendar(){
			$myStr="<table class='calendar' cellpadding='0' cellspacing='0'>
					<tr><th></th>
					<th >";
			$ma=$this->actualMon-1;
			if($ma==0){
				$pstYear = $this->year-1;
				$myStr.="<a href='{$this->enlace}.php?Mes=12&a={$pstYear}'>";
			}
			else $myStr.="<a href='{$this->enlace}.php?Mes={$ma}&a={$this->year}'>";
			$myStr.="<img border='0' src='../images/temp/backButton.jpg' /></a></th>
					<th colspan='3' >{$this->months[($this->actualMon)-1]} {$this->year}</th>
					<th >";
			$ms=$this->actualMon+1;
			if($ms<13)
				$myStr.="<a href='{$this->enlace}.php?Mes={$ms}&a={$this->year}'>";
			else{
				$nxtYear=$this->year+1;
			 	$myStr.="<a href='{$this->enlace}.php?Mes=1&a={$nxtYear}'>";
			}
			$myStr.="<img border='0' src='../images/temp/forwardButton.jpg' /></a></th>
					<th ></th></tr><tr>";
			foreach($this->week as $day){
				$myStr.="<td>&nbsp;{$day}&nbsp;</td>";
			}
			$myStr.="</tr>";
			$BeginDay=$this->primerDiaMes($this->actualMon,$this->year);
			$begin=false;
			for($i=0;$i<6;$i++){ //cambiar while
				$myStr.="<tr>";
				for($z=0;$z<7;$z++){
					if($z==$BeginDay){
						$BeginDay=100;
						$days=1;
						$myStr.="<td>{$days}<br /><span>&nbsp;</span></td>";
						$begin=true;
					}elseif($begin){
						if($this->days==$days){
							$begin=false;
						}else{
							$days++;
							$myStr.="<td>{$days}<br /><span>&nbsp;</span></td>";
						}
					}else
						$myStr.="<td><br /><span>&nbsp;</span></td>";
				}
				$myStr.="</tr>";
				if(!$begin)
					break;
			}
			$myStr.="</table>";
			return $myStr;
		}
		
		function primerDiaMes($m,$a){
			$dia=date("w", mktime(0,0,0,$m,1,$a));
			return $dia; 
		}
	}
?>